import { request } from '../request';

/**
 * @desc 学习图表数据
 */
export type Chartdata = {
  createTime: string;
  noStudyPersonNum: number;
  studyPersonNum: number;
};
export type StudyInfoItemType = {
  title: string;
  studyInfoVoList: Chartdata[];
};
/**
 * @desc ai学习已开启的列表
 */
export async function getAiList() {
  const { data } = await request<StudyInfoItemType[]>({
    url: '/safe/api/aiPlanTask/statistics/lastTenDaysList',
    method: 'get',
    noAutoShowErrorMessage: true,
  });
  return data;
}
export type AiPlanListItem = {
  title: string;
  No: number;
  startTime: string;
  endTime: string;
  allPersonNum: number;
  studyPersonNum: number;
  noStudyPersonNum: number;
  studyRatio: string;
  aiPlanId: string;
  aiCycleId: string;
  studyObject: number;
};
/**
 * @desc ai学习情况列表 只显示昨天的周期
 * 小幺鸡地址 http://apidoc.scm.logibeat.com/xiaoyaoji/doc/5FKErNSVM
 */
export async function getAllAiStudyList(pageIndex = 1, pageSize = 10) {
  const { data } = await request<{ list: AiPlanListItem[]; total: number }>({
    url: '/safe/api/aiPlanTask/statistics/yesterdayList',
    data: {
      pageIndex: pageIndex,
      pageSize: pageSize,
    },
    noAutoShowErrorMessage: true,
  });
  data.list.forEach((e, index) => {
    //编号
    e.No = (pageIndex - 1) * pageSize + (index + 1);
  });
  return data;
}

export type PlanPersonListItem = {
  No: number;
  personName: string;
  phone: string;
  positionName: string;
  finishState: number;
};
/**
 * @desc 计划未学习人员学习明细
 * 小幺鸡地址 http://apidoc.scm.logibeat.com/xiaoyaoji/doc/5Fk3UWAYM
 */
export async function getPersonStudyDetailList(url: string, postParams: any, pageIndex = 1, pageSize = 10) {
  const { data } = await request<{ list: PlanPersonListItem[]; total: number }>({
    url: url,
    data: {
      pageNum: pageIndex,
      pageSize: pageSize,
      ...postParams,
    },
    noAutoShowErrorMessage: true,
  });
  // const data = {
  //   list: [
  //     {
  //       No: 0,
  //       personName: '张三',
  //       phone: '18069446935',
  //       positionName: 'ganfg',
  //       finishState: 2,
  //     },
  //   ],
  //   total: 1,
  // };
  data.list.forEach((e, index) => {
    //编号
    e.No = (pageIndex - 1) * pageSize + (index + 1);
  });
  return data;
}

/**
 * @desc 获取ai学习下发的企业和协会下拉列表数据
 */
type entType = {
  entId: string;
  entName: string;
};
type AssociationOrEntType = {
  associationEntList: entType[];
  entList: entType[];
};
export async function associationandEntList(params: any) {
  const { data } = await request<AssociationOrEntType>({
    url: '/safe/api/aiPlanTask/statistics/distributeAssociationOrEnt',
    method: 'get',
    params: params,
    noAutoShowErrorMessage: true,
  });
  return data;
}
/**
 * @desc 企业下拉列表数据
 */
// export async function entList(params: any) {
//   //   const { data } = await request({
//   //     url: '/safe/api/statistics/getCourseList',
//   //     params: params,
//   //     noAutoShowErrorMessage: true,
//   //   });
//   const data = [
//     {
//       memberId: '3333',
//       memberName: '企业1',
//     },
//     {
//       memberId: '4444',
//       memberName: '企业2',
//     },
//   ];
//   return data;
// }
/**
 * @desc ai学习计划下发的企业列表
 * 小幺鸡地址 http://apidoc.scm.logibeat.com/xiaoyaoji/doc/5Ftq49t61
 */
export type StudyEntType = {
  No: number;
  entName: string;
  ownerAssociation: string;
  positionName: string;
  allPersonNum: number;
  studyPersonNum: number;
  noStudyPersonNum: number;
  studyRatio: string;
  entId: string;
  aiPlanId: string;
  aiCycleId: string;
};
export type StudyEntSearch = {
  aiCycleId: string;
  aiPlanId: string;
  associationName?: string;
  entName?: string;
};
export async function getAiStudyEntList(pageNum: number, pageSize: number, params: any) {
  const { data } = await request<{ list: StudyEntType[]; total: number }>({
    url: '/safe/api/aiPlanTask/statistics/aiStudyEntSituationList',
    data: {
      pageNum,
      pageSize,
      ...params,
    },
    noAutoShowErrorMessage: true,
  });
  data.list.forEach((e, index) => {
    //编号
    e.No = index + 1;
  });
  return data;
}

/**
 *
 * 根据ai计划id获取学习时间
 *小幺鸡：http://apidoc.scm.logibeat.com/xiaoyaoji/doc/5FSNuzhuj
 */
export interface CycleList {
  aiCycleId: string;
  startTime: string;
  endTime: string;
}
export async function getCycleByPlan(aiPlanId: string) {
  const { data } = await request<CycleList[]>({
    url: '/safe/api/aiPlanTask/statistics/getAiStudyCycle',
    params: {
      aiPlanId,
    },
  });
  return data;
}

export type HistoryItemType = {
  No?: number;
  title?: string;
  startTime?: string;
  endTime?: string;
  personName?: string;
  phone?: string;
  entName?: string;
  finshTime?: string;
  finishState?: string;
  aiPlanId?: string;
};
export type HistoryListSearchParams = {
  aiCycleId?: string;
  aiPlanId?: string;
  finishState?: string | number;
};
/**
 * ai学习历史情况列表
 */
export async function getAiHistoryStudyList(
  { aiCycleId, aiPlanId, finishState }: Partial<HistoryListSearchParams>,
  pageIndex = 1,
  pageSize = 10,
) {
  const { data } = await request<{ list: HistoryItemType[]; total: number }>({
    url: '/safe/api/aiPlanTask/statistics/historyAiStudyList',
    data: {
      pageIndex,
      pageSize,
      aiCycleId,
      aiPlanId,
      finishState,
    },
    noAutoShowErrorMessage: true,
  });
  data.list.forEach((e, index) => {
    e.No = index + 1;
  });
  return data;
}

/**
 *导出ai学习历史数据
 */
export function exportAiHistoryList({ aiCycleId, aiPlanId, finishState }: HistoryListSearchParams) {
  const downUrl =
    location.origin +
    `/safe/api/aiPlanTask/statistics/historyAiStudyListExport?aiCycleId=${aiCycleId}&aiPlanId=${aiPlanId}&finishState=${
      finishState || ''
    }&pageIndex=0&pageSize=999999`;

  console.log('downUrl', downUrl);
  // const a = document.createElement('a');
  // a.target = '_blank';
  // a.href = downUrl;
  // a.click();
  window.open(downUrl);
}
